# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=libcoro
pkgver=6.513
pkgrel=0
pkgdesc="Coroutines (cooperative multitasking) in a portable fashion implemented in C"
url="http://software.schmorp.de/pkg/libcoro.html"
arch="all"
license="BSD-2-Clause"
makedepends="cvs gzip musl-dev"
subpackages="$pkgname-dev"
source="https://dev.alpinelinux.org/archive/$pkgname/$pkgname-$pkgver.tar.gz
	test.c"

_cvsurl="cvs.schmorp.de/schmorpforge"
_cvstag="rel-${pkgver/./_}"

snapshot() {
	local tarball="$pkgname-$pkgver.tar.gz"

	mkdir -p "$srcdir"
	cd "$srcdir"

	msg "Creating $tarball"
	cvs -z3 -d :pserver:anonymous@$_cvsurl export -r $_cvstag \
		-d ${tarball%.tar.gz} $pkgname

	# Create reproducible tarball (always same checksum for same content).
	tar -c --owner=0 --group=0 --mtime='2017-07-15' ${tarball%.tar.gz} \
		| gzip -n -9 > "$SRCDEST"/$tarball

	msg "Uploading $tarball"
	scp "$SRCDEST"/$tarball dev.alpinelinux.org:/archive/$pkgname/
}

prepare() {
	default_prepare
	ln -s "$srcdir"/test.c "$builddir"/
}

build() {
	CFLAGS="-fPIC -pedantic -Wall"
	case "$CARCH" in
		x86*) CFLAGS="$CFLAGS -DCORO_ASM";;
		*) CFLAGS="$CFLAGS -DCORO_SJLJ";;
	esac

	# Build shared lib.
	$CC $CFLAGS -O3 -c coro.c
	$CC $LDFLAGS -shared -o libcoro.so coro.o

	# Build static lib.
	$CC $CFLAGS -c coro.c -o coro.o
	ar rc libcoro.a coro.o
	ranlib libcoro.a

	# Build test.
	$CC $CFLAGS $LDFLAGS -L. test.c -o test -lcoro
}

check() {
	LD_LIBRARY_PATH=. ./test
}

package() {
	local abiver=$(sed -En 's/#define\s*CORO_VERSION\s*(\d+)/\1/p' coro.h)
	local soname="libcoro.so.$abiver"

	install -D -m 755 ${soname%.$abiver} "$pkgdir"/usr/lib/$soname
	ln -s $soname "$pkgdir"/usr/lib/${soname%.$abiver}

	install -D -m 644 libcoro.a "$pkgdir"/usr/lib/libcoro.a
	install -D -m 644 coro.h "$pkgdir"/usr/include/coro.h
}

sha512sums="e4d0bb21333d4fe4f8afdcd8a8b7bb27a97bf30727e2425d9cb04d6b01934972d65c7f9d90fe30f83a933b6e863c8f85377b87945f6739d7bc409602c0d2fbac  libcoro-6.513.tar.gz
643cdbc3e4a9b48d05cd54b9cf436652abd41485def821cfaf682c5efc77c63c9aaff0b40dbaad17c87f9ef16f7b3739360c9d28fa15d61b620f0508fe5a7701  test.c"
